package com.fangcun.controller.api.old;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.api.order.request.GoodsCreateOrderRequest;
import com.fangcun.bean.api.order.request.OrderApplyRefundRequest;
import com.fangcun.bean.api.order.request.OrderDetailRequest;
import com.fangcun.bean.api.order.request.OrderListRequest;
import com.fangcun.bean.api.order.request.OrderRevokeRefundRequest;
import com.fangcun.bean.api.order.request.ShopCartCreateOrderRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.service.api.OrderApiService;

/**
 * @作者： 陳曉鬆 @时间： 2020年7月16日 - 上午11:44:04 @description：->
 */
@RestController
@RequestMapping("/api/{version}/auth/order")
public class OrderApiController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(OrderApiController.class);

	@Resource(name = "orderApiService")
	private OrderApiService orderApiService;
	
	/** 购物车创建订单 */
	@ApiVersion(1.0)
	@RequestMapping("shopcartCreateOrder")
	public ResponseMsg addShopCart(@RequestBody ShopCartCreateOrderRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(orderApiService.shopCartCreateOrder(request));
		} catch (ProAffairException e) {
			LOG.error("-订单-购物车创建订单出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-购物车创建订单异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	/** 商品创建订单 */
	@ApiVersion(1.0)
	@RequestMapping("goodsCreateOrder")
	public ResponseMsg goodsCreateOrder(@RequestBody GoodsCreateOrderRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(orderApiService.createOrderByGoods(request));
		} catch (ProAffairException e) {
			LOG.error("-订单-商品创建订单出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-商品创建订单异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 订单列表 */
	@ApiVersion(1.0)
	@RequestMapping("list")
	public ResponseMsg list(@RequestBody PageBean<OrderListRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setUserId(getApiUserId());
			msg.setData(orderApiService.list(request));
		} catch (ProAffairException e) {
			LOG.error("-订单-订单列表出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-订单列表异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 订单详情 */
	@ApiVersion(1.0)
	@RequestMapping("detail")
	public ResponseMsg detail(@RequestBody OrderDetailRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(orderApiService.detail(request));
		} catch (ProAffairException e) {
			LOG.error("-订单-订单详情出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-订单详情异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 确认收货 */
	@ApiVersion(1.0)
	@RequestMapping("confirmReceivingGoods")
	public ResponseMsg confirmReceivingGoods(Long orderId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderApiService.confirmReceivingGoods(orderId, getApiUserId());
		} catch (ProAffairException e) {
			LOG.error("-订单-确认收货出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-确认收货异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 删除订单 */
	@ApiVersion(1.0)
	@RequestMapping("delete")
	public ResponseMsg delete(@RequestBody List<Long> orderIds) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderApiService.updateOrderByIsdelete(orderIds);
		} catch (ProAffairException e) {
			LOG.error("-订单-删除订单出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-删除订单异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 获取用户订单每个状态的数量 */
	@ApiVersion(1.0)
	@RequestMapping("getOrderStatusNumber")
	public ResponseMsg getOrderStatusNumber() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderApiService.getOrderStatusNumber(getApiUserId()));
		} catch (ProAffairException e) {
			LOG.error("-订单-获取用户订单每个状态的数量出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-获取用户订单每个状态的数量异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 申请退换款 */
	@ApiVersion(1.0)
	@RequestMapping("applyRefund")
	public ResponseMsg applyRefund(@RequestBody OrderApplyRefundRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(orderApiService.applyRefund(request));
		} catch (ProAffairException e) {
			LOG.error("-订单-申请退款出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-申请退款异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 撤销申请退货 */
	@ApiVersion(1.0)
	@RequestMapping("revokeRefund")
	public ResponseMsg revokeRefund(@RequestBody OrderRevokeRefundRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			orderApiService.revokeRefund(request);
		} catch (ProAffairException e) {
			LOG.error("-订单-撤销申请退货出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-撤销申请退货异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 订单支付 */
	@ApiVersion(1.0)
	@RequestMapping("pay")
	public ResponseMsg pay(String orderNumber, Integer payType, Integer source) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderApiService.pay(orderNumber, getApiUserId(), payType, source));
		} catch (ProAffairException e) {
			LOG.error("-订单-订单支付出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单-订单支付异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
